unit Ticker;

interface

uses
  Database, AM, Functions, VarGrabber,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, Buttons, Menus, DB, Grids, ExtCtrls, Mask, DBTables,
  DBGrids, JvExMask, JvSpin, JvDBSpinEdit, DBCtrls;

type
  TFormTicker = class(TForm)
    BtnDone: TBitBtn;
    BtnHelp: TBitBtn;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    Filter: TLabel;
    EditAction: TDBEdit;
    BtnAdd: TBitBtn;
    BtnDelete: TBitBtn;
    Label1: TLabel;
    BtnTicker: TBitBtn;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Bevel4: TBevel;
    Ctrla1: TMenuItem;
    Panel1: TPanel;
    Status: TLabel;
    BtnAM: TBitBtn;
    BtnFunctions: TBitBtn;
    BtnVar: TBitBtn;
    BtnClearCommand: TBitBtn;
    CtrlR1: TMenuItem;
    CtrlF1: TMenuItem;
    EditCopy: TEdit;
    DSTicker: TDataSource;
    GridTicker: TDBGrid;
    SpinTick: TJvDBSpinEdit;
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnDoneClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure SpinTickExit(Sender: TObject);
    procedure SpinTickKeyPress(Sender: TObject; var Key: Char);
    procedure EditActionKeyPress(Sender: TObject; var Key: Char);
    procedure BtnDeleteClick(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnTickerClick(Sender: TObject);
    procedure GridTickerDblClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnAMClick(Sender: TObject);
    procedure BtnFunctionsClick(Sender: TObject);
    procedure BtnVarClick(Sender: TObject);
    procedure BtnClearCommandClick(Sender: TObject);
  private
    function GetNewTicker: integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormTicker: TFormTicker;

implementation
uses
   Variants, DBClient, SqlExpr;

{$R *.DFM}

procedure TFormTicker.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormTicker.BtnDoneClick(Sender: TObject);
begin
  datawell.TableTicker.ApplyUpdates(0);
  close;
end;

procedure TFormTicker.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('ticker');
end;

procedure TFormTicker.SpinTickExit(Sender: TObject);
begin
  if SpinTick.value < 2 then SpinTick.value:=2;
end;

procedure TFormTicker.SpinTickKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    EditAction.setfocus;
  end;
end;

procedure TFormTicker.EditActionKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnAdd.click;
  end;
end;

procedure TFormTicker.BtnDeleteClick(Sender: TObject);
begin
  if not Datawell.TableTicker.EOF then Datawell.TableTicker.delete;
  if (Datawell.TableTicker.fieldbyname('Tick').asinteger < 1) and
    (BtnTicker.font.color = clred) then BtnTicker.click;
end;

function TFormTicker.GetNewTicker: integer;
var
   ds: TCustomSQLDataSet;
begin
   ds := nil;
   datawell.dbFirebird.Execute('select coalesce(max(ID), 0) + 1 as RESULT from TICKER', nil, @ds);
   try
      result := ds['RESULT'];
   finally
      ds.Free;
   end;
end;

procedure TFormTicker.BtnAddClick(Sender: TObject);
var
  i: integer;
  didfind: boolean;
  table: TCustomClientDataset;
begin
  didfind:=false;
  i := Datawell.TableTicker.RecordCount;
  if i >= 50 then begin
    messagedlg('You can only have 50 maximum tickers',
      mtinformation,[mbok],0);
    BtnDelete.setfocus;
    exit;
  end;
  if EditAction.text = '' then begin
    if messagedlg('Enter a blank command?',mtinformation,[mbyes,mbcancel],0) = mrcancel then begin
      EditAction.setfocus;
      exit;
    end;
  end;
  if pos('"',EditAction.text) > 0 then begin
    messagedlg('Ticker actions cannot contain quotes',mtinformation,[mbok],0);
    EditAction.setfocus;
    exit;
  end;
  EditAction.text:=Datawell.CutTrailingSpaces(EditAction.text);
  table := Datawell.TableTicker;
  didfind := table.Locate('Tick;Action', VarArrayOf([trunc(SpinTick.value), EditAction.text]), []);
  if didfind then begin
    table.edit;
    table.fieldbyname('Tick').asinteger:=trunc(SpinTick.value);
    table.fieldbyname('Action').asstring:=EditAction.text;
    table.post;
  end
  else begin
    Datawell.TableTicker.append;
    Datawell.TableTicker.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableTicker.fieldbyname('ID').asinteger:=GetNewTicker;
    Datawell.TableTicker.fieldbyname('Tick').asinteger:=trunc(SpinTick.value);
    Datawell.TableTicker.fieldbyname('Action').asstring:=EditAction.text;
    Datawell.TableTicker.post;
  end;
  EditAction.text:='';
  SpinTick.setfocus;
  Datawell.TableTicker.ApplyUpdates(0);
end;

procedure TFormTicker.BtnTickerClick(Sender: TObject);
var val: boolean;
begin
  if BtnTicker.font.color = clgreen then begin
    if Datawell.TableTicker.fieldbyname('Tick').asinteger < 1 then begin
      messagedlg('There are no tickers defined',mtinformation,[mbok],0);
      exit;
    end;
    BtnTicker.caption:='Disable Tic&ker';
    BtnTicker.font.color:=clred;
    val:=true;
  end
  else begin
    BtnTicker.caption:='Enable Tic&ker';
    BtnTicker.font.color:=clgreen;
    val:=false;
  end;
  Datawell.TableOptions.edit;
  Datawell.TableOptions.fieldbyname('EnableTicker').asboolean:=val;
  Datawell.TableOptions.post;
end;

procedure TFormTicker.GridTickerDblClick(Sender: TObject);
begin
  SpinTick.value:=Datawell.TableTicker.fieldbyname('Tick').asinteger;
  EditAction.text:=Datawell.TableTicker.fieldbyname('Action').asstring;
end;

procedure TFormTicker.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridTicker) then begin
    BtnDelete.click;
    key:=0;
  end;
end;

procedure TFormTicker.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  am: string;
begin
  form:=TFormAM.create(self);
  form.setup;
  form.showmodal;
  am:=form.am;
  form.free;
  if am = '' then exit;
  EditAction.text:=am;
  EditAction.setfocus;
end;

procedure TFormTicker.BtnFunctionsClick(Sender: TObject);
var
  form: TFormFunctions;
  i, po: integer;
begin
  form:=TFormFunctions.create(self);
  form.Setup(Datawell.lastfunction,EditAction.seltext);
  form.showmodal;
  po:=form.po;
  form.free;
  i:=EditAction.selstart;
  if po < 1 then begin
    EditAction.setfocus;
    exit;
  end;
  EditAction.pastefromclipboard;
  EditAction.setfocus;
  EditAction.sellength:=0;
  EditAction.selstart:=i+po;
end;

procedure TFormTicker.BtnVarClick(Sender: TObject);
var
  form: TFormVarGrab;
  grab: string;
  i, l: integer;
begin
  form:=TFormVarGrab.create(self);
  form.setup;
  form.showmodal;
  grab:=form.grab;
  form.free;
  i:=EditAction.selstart;
  l:=EditAction.sellength;
  if grab = '' then begin
    EditAction.setfocus;
    EditAction.selstart:=i;
    EditAction.sellength:=l;
    exit;
  end;
  EditCopy.text:=grab;
  EditCopy.selectall;
  EditCopy.copytoclipboard;
  EditAction.pastefromclipboard;
  EditAction.setfocus;
  EditAction.sellength:=0;
  EditAction.selstart:=i+length(EditCopy.text);
end;

procedure TFormTicker.BtnClearCommandClick(Sender: TObject);
begin
  EditAction.text:='';
  EditAction.setfocus;
end;

end.
